package com.qianfeng.gl4study.youshare.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.qianfeng.gl4study.youshare.model.CollectItem;

import java.util.LinkedList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * I'm glad to share my knowledge with you all.
 * User:Gaolei
 * Date:2015/2/26
 * Email:pdsfgl@live.com
 */
public class CollectDatabasesManager {
	private static CollectDatabasesManager ourInstance;

	private CollectDatabasesHelper dbHelper;
	private SQLiteDatabase database;
	private boolean ret = false;

	public static CollectDatabasesManager createInstance(Context context){
		if(ourInstance==null){
			ourInstance = new CollectDatabasesManager(context);
		}
		return ourInstance;
	}

	public static CollectDatabasesManager getInstance() {
		return ourInstance;
	}

	Context context;
	private CollectDatabasesManager(Context context) {
		this.context = context;
	}


	/**
	 * 查询商品收藏列表
	 * @param userId
	 * @return
	 */
	public Cursor queryProductFavorites(String userId){
		SQLiteDatabase db = openDatabases();
		Cursor query = db.query(
				CollectDatabasesHelper.TABLE_COLLECT_ITEM,
				null,
				"user_id = ?",
				new String[]{userId},
				null, null, null
		);
		return query;
	}

	/**
	 * 根据userId获取商品收藏列表的信息
	 * @param userId
	 * @return
	 */
	public LinkedList<CollectItem> getProductCollect(String userId){

		LinkedList<CollectItem> ret = null;
		SQLiteDatabase db = openDatabases();
		Cursor query = db.query(
				CollectDatabasesHelper.TABLE_COLLECT_ITEM,
				null,
				"user_id = ?",
				new String[]{userId},
				null, null, null
		);
		if(query!=null){
			ret = new LinkedList<CollectItem>();
			while (query.moveToNext()){
				CollectItem collectItem = new CollectItem();
				collectItem.parseCursor(query);
				ret.add(collectItem);
			}
			query.close();
		}
		return ret;
	}

	/**
	 * 收藏商品
	 * @param collectItem
	 * @param userId 当前收藏的用户的Id
	 * @return
	 */
	public boolean collectProduct(CollectItem collectItem,String userId){

		/*检查数据库是否打开
		未打开进行打开操作
		*/

		SQLiteDatabase db = openDatabases();
		if(db!=null){
			if(collectItem!=null) {
				try {
					db.beginTransaction();
					ContentValues values = new ContentValues();
					values.put("productId",collectItem.getProductId());
					values.put("productName",collectItem.getProductName());
					values.put("productPrice",collectItem.getProductPrice());
					values.put("productThumb",collectItem.getProductThumb());
					values.put("count",collectItem.getCount());
					int checked = 0;
					if(collectItem.isChecked()){
						checked = 1;
					}
					values.put("checked",checked);
					values.put("user_id",userId);
					long rid = db.insert(CollectDatabasesHelper.TABLE_COLLECT_ITEM, null, values);
					ret = rid!=-1;
					if(ret){
						db.setTransactionSuccessful();
					}
				}catch (Exception ex){
					ex.printStackTrace();
				}finally {
					db.endTransaction();
				}
			}
		}
		return ret;
	}

	/**
	 * 打开数据库的操作
	 * @return
	 */
	private SQLiteDatabase openDatabases(){

		if(database == null){
			if(dbHelper==null) {
				dbHelper = new CollectDatabasesHelper(context);
			}
			database = dbHelper.getWritableDatabase();
		}
		return database;
	}


}
